
package com.be.beadmin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.service.dto.input.FactoryCalendarDto;
import com.be.beadmin.service.dto.output.FactoryCalendarEx;
import com.be.beadmin.service.entity.FactoryCalendar;
import com.be.beadmin.service.entity.FactoryCalendar_;
import com.be.beadmin.service.mapper.FactoryCalendarMapper;
import com.be.beadmin.service.mapper.FactoryCalendarMapperEx;
import com.be.beadmin.service.repository.FactoryCalendarRepository;
import com.be.beadmin.service.service.FactoryCalendarService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author chenwei
* @date 2023-02-22
**/
@Service
@RequiredArgsConstructor
public class FactoryCalendarServiceImpl implements FactoryCalendarService {

    @Autowired
    private  FactoryCalendarRepository factoryCalendarRepository;
    @Autowired
    private  FactoryCalendarMapper factoryCalendarMapper;
    @Autowired
    private  FactoryCalendarMapperEx factoryCalendarExMapper;

    @Override
    public Page<FactoryCalendarEx> getFactoryCalendarByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<FactoryCalendar> page = factoryCalendarRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(FactoryCalendar_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(FactoryCalendar_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<FactoryCalendarEx> page1 = page.map(item -> {
                        FactoryCalendarEx factoryCalendarEx =   new FactoryCalendarEx();
                        factoryCalendarEx = factoryCalendarExMapper.toEntity(item);
                        factoryCalendarEx.setCalendarName(item.getCalendarName());
                        return factoryCalendarEx;
                    }

            );
            return page1;

    }

    @Override
    public void addFactoryCalendar(FactoryCalendarDto factoryCalendarDto) {
            FactoryCalendar result = factoryCalendarRepository.findFactoryCalendarByIdAndDelete(factoryCalendarDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("FactoryCalendar不存在,请重新查询");
            }
            FactoryCalendar factoryCalendar = factoryCalendarMapper.toEntity(factoryCalendarDto);
            factoryCalendar.setCalendarName(factoryCalendarDto.getCalendarName());
            factoryCalendarRepository.save(factoryCalendar);
    }

    @Override
    public void updateFactoryCalendar(FactoryCalendarDto factoryCalendarDto) {
            FactoryCalendar factoryCalendar = factoryCalendarRepository.findFactoryCalendarById(factoryCalendarDto.getId());
            factoryCalendar = factoryCalendarMapper.toEntity(factoryCalendarDto);
            factoryCalendarRepository.save(factoryCalendar);
    }


    @Override
    public void delFactoryCalendar(String id) {
        FactoryCalendar factoryCalendar = factoryCalendarRepository.findFactoryCalendarById(id);
        factoryCalendar.setDelete(1);
        factoryCalendarRepository.save(factoryCalendar);
    }

    @Override
    public void delFactoryCalendars (List<String> ids) { }
}